package com.haipumi.servlets.backadmin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import com.haipumi.beans.JsonModel;
import com.haipumi.beans.haipumi_activity;
import com.haipumi.beans.haipumi_product;
import com.haipumi.beans.haipumi_skin;
import com.haipumi.beans.haipumi_user;
import com.haipumi.biz.GoodsBiz;
import com.haipumi.biz.IBackAdminBiz;
import com.haipumi.biz.IBackCustBiz;
import com.haipumi.biz.impl.BackAdminBizImpl;
import com.haipumi.biz.impl.BackCustBizImpl;
import com.haipumi.biz.impl.GoodsBizImpl;
import com.haipumi.servlets.CommonServlet;
import com.haipumi.utils.CommonData;
import com.haipumi.utils.FileUploadUtil;


public class FileUploadServlet extends CommonServlet {

	
	private IBackCustBiz ibab=new BackCustBizImpl();
	private GoodsBiz gb=new GoodsBizImpl();
	/**
	 * 
	 */
	private static final long serialVersionUID = 4844116965223455305L;

	/**
	 * 通用上传的封装
	 */
	private Map<String,List<String>> commonUpload(HttpServletRequest request, HttpServletResponse response,String fileaddress){
		//用smartupload取到一些参数，可能不包含op  并把收到的图片调用smartupload来存到服务器并返回新的参数和图片名
		Map<String,List<String>> pvalues=null;
		PageContext pg=javax.servlet.jsp.JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 8129, true);
		FileUploadUtil fuu=new FileUploadUtil();
		try {
			pvalues=fuu.fileUpload(pg, fileaddress);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//这一步完成了图片的上传
		return pvalues;
	}
	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op=request.getParameter("op");
		
		//TODO 这样不能传递参数
		String addrDir="";
		if(request.getParameter("addrDir")!=null && !"".equals(request.getParameter("addrDir")) ){
			addrDir=request.getParameter("addrDir")+"/";//存 储位置
		}
		
		if(op!=null){
			//由ckeditor上传
			if("ckeditorupload".equals(op)){
				Map<String,List<String>> pvalues=commonUpload(request,response,"images/mixedPicData/");
				if(pvalues!=null){
					ckeditorupload(pvalues,request,response);
				}else{
					response.setCharacterEncoding("utf-8");
					PrintWriter pw=response.getWriter();
					pw.print("<font color=\"red\" >文件格式不正确,必须为.jpg/.gif/等，还没写好</font>");
				}
			}
		}else{
			try {
				Map<String ,List<String>>pvalues=commonUpload(request, response, "images/mixedPicData/"+addrDir);
				List<String> ops=pvalues.get("op");
				op=ops.get(0);
				if("userheadimage".equals(op)){
					userheadimage(pvalues,request,response);
				}else if("goodspphotoupload".equals(op)){
					goodspphotoupload(pvalues,request,response);
				}else if("userskin".equals(op)){
					userskin(pvalues,request,response);
				}else if("restoredatabase".equals(op)){
					restoredatabase(pvalues,request,response);
				}else if("databaserestore".equals(op)){
					databaserestore(pvalues,request,response);
				}else if("acphoto".equals(op)){
					acphoto(pvalues,request,response);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	private void acphoto(Map<String, List<String>> pvalues,
			HttpServletRequest request, HttpServletResponse response) {
		int acid=Integer.parseInt(pvalues.get("acid").get(0));
		String acphoto=pvalues.get("up_img").get(1);//这里是从pvalues中取出的uimage的文件名
		
		haipumi_activity activity=new haipumi_activity();
		activity.setAcid(Integer.toString(acid));
		activity.setAcphoto(acphoto);
		
		JsonModel jm=new JsonModel();
		haipumi_activity backactivity=null;
		backactivity=ibab.acphoto(activity);
		super.outData(response, backactivity);
	}


	private void databaserestore(Map<String, List<String>> pvalues,
			HttpServletRequest request, HttpServletResponse response) {
		
	}


	private void restoredatabase(Map<String, List<String>> pvalues,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		
	}


	private void userskin(Map<String, List<String>> pvalues,
			HttpServletRequest request, HttpServletResponse response) {
		int sid=Integer.parseInt(pvalues.get("sid").get(0));
		String simage=pvalues.get("newskin").get(1);//这里是从pvalues中取出的uimage的文件名
		
		haipumi_skin skin=new haipumi_skin();
		skin.setSid(Integer.toString(sid));
		skin.setSimage(simage);
		
		JsonModel jm=new JsonModel();
		haipumi_skin newskin=null;
		newskin=ibab.updateSimage(skin);
		super.outData(response, newskin);
	}


	private void goodspphotoupload(Map<String, List<String>> pvalues,
			HttpServletRequest request, HttpServletResponse response) {
		int pid=Integer.parseInt(pvalues.get("pid").get(0));
		String pphoto=pvalues.get("newpphoto").get(1);//这里是从pvalues中取出的newpphoto的文件名
		haipumi_product goods=new haipumi_product();
		goods.setPid(Integer.toString(pid));
		goods.setPphoto(pphoto);
		JsonModel jm=new JsonModel();
		boolean flag=gb.updateGoodsPic(goods);
		haipumi_product returnGoods=null;
		if(flag){
			ServletContext application=(ServletContext) request.getSession().getServletContext();
			if(application.getAttribute(CommonData.APPLICATIONPRODUCTKEY)!=null){
				List<haipumi_product> list=(List<haipumi_product>) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONPRODUCTKEY);
				for(int i=0;i<list.size();i++){
					if(list.get(i).getPid()==goods.getPid()){
						list.get(i).setPphoto(goods.getPphoto());
						returnGoods=list.get(i);
						break;
					}
				}
				request.getSession().getServletContext().setAttribute(CommonData.APPLICATIONPRODUCTKEY, list);
			}
		}
		super.outData(response, returnGoods);
	}


	private void userheadimage(Map<String, List<String>> pvalues,
			HttpServletRequest request, HttpServletResponse response) {
		int uid=Integer.parseInt(pvalues.get("uid").get(0));
		String uimage=pvalues.get("up_img").get(1);//这里是从pvalues中取出的uimage的文件名
		
		haipumi_user user=new haipumi_user();
		user.setUid(Integer.toString(uid));
		user.setUimage(uimage);
		
		JsonModel jm=new JsonModel();
		haipumi_user backuser=null;
		backuser=ibab.updateUserImage(user);
		super.outData(response, backuser);
	}


	/**
	 * ckeditor 上传过来的文件，并且要回调ck中的回调函数，以在混排中显示图片
	 * @param pvalues
	 * @param request
	 * @param response
	 */
	private void ckeditorupload(Map<String,List<String>>pvalues,HttpServletRequest request,
			HttpServletResponse response) {
		String fileName=pvalues.get("upload").get(1);
		String callBack=request.getParameter("CKEditorFuncNum");//回调函数的名字
		try {
			PrintWriter out=response.getWriter();
			out.println("<script type=\"text/javascript\">");
			out.println("window.parent.CKEDITOR.tools.callFunction("+callBack+",'images/mixedPicData/"+fileName+"')");//callFunction(callBack,'images/fileName')
			out.println("</script>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
